package com.dgtech.sss.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.bt_con).setOnClickListener(this);
        et_name = findViewById(R.id.et_name);
        et_password = findViewById(R.id.et_password);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_con:
                final String name = et_name.getText().toString();
                final String password = et_password.getText().toString();
                if (name==null || TextUtils.isEmpty(name)){
                    Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password==null || TextUtils.isEmpty(password)){
                    Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                new Thread(){
                    @Override
                    public void run() {
                        try {
                            //步骤①创建Socket对象,指明需要连接的服务器的地址和端口号
                            //host指的是服务器的地址,因为服务器和客户端装在一个手机上,
                            Socket socket=new Socket("localhost", 43211);
                            //步骤②连接建立后,通过输出流向服务器发送请求信息
                            OutputStream os=socket.getOutputStream();
                            PrintWriter pw=new PrintWriter(os);
                            //写入用户名和密码
                            pw.write(name);
                            //用:来分割用户名和密码
                            pw.write(":");
                            pw.write(password);
                            pw.flush();
                            socket.shutdownOutput();
                            /*//注意!!!!
                            //上面为了简单写用的是字符串,实际开发中用的对象较多,也就是如下写法
                            ObjectOutputStream oos = new ObjectOutputStream(os);
                            User user = new User(name,password);
                            oos.writeObject(user);*/


                            //设置超时时间,如果在5s没没有收到服务器的返回,则异常
                            socket.setSoTimeout(1000*5);
                            //步骤③通过输入流获取服务器的响应信息
                            InputStream is=socket.getInputStream();
                            BufferedReader br=new BufferedReader(new InputStreamReader(is));
                            String info=null;
                            while((info=br.readLine())!=null){
                                System.out.println(info);
                            }
                            //步骤④关闭相关资源
                            br.close();
                            is.close();
                            pw.close();
                            os.close();
                            socket.close();
                        } catch (UnknownHostException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
        }
    }
}
